package com.ujiuye.controller;

import com.ujiuye.pojo.Member;
import com.ujiuye.pojo.Setmeal;
import com.ujiuye.service.IMemberService;
import com.ujiuye.utils.*;
import com.ujiuye.vo.MemberVo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private IMemberService memberService;

    @PostMapping("/list")
    public PageResult list(@RequestBody QueryPageBean queryPageBean){
        try {
            return memberService.listPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping("/years")
    public List<Object> years(){
        try {
            return memberService.getAllYear();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") int id){
        try {
            memberService.removeById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @PostMapping("/add")
    public Result save(@RequestBody Member member){
        try {
            memberService.save(member);
            return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @GetMapping("/{id}")//通过id查询检查项  用户修改时回显
    public Result getById(@PathVariable("id") int id){
        try {
            Member member = memberService.getById(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PutMapping("/update")//执行修改
    public Result update(@RequestBody Member member){
        try {
            memberService.updateById(member);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @Value("${uploadPath}")
    private String path;

    @RequestMapping("/downloadexcel/{currentPage}/{pageSize}")
    public ResponseEntity<byte[]> downloadexcel(@PathVariable("currentPage") Integer currentPage,@PathVariable("pageSize") Integer pageSize) throws IOException {
        //后台查询当页会员信息
        List<Member> list = memberService.listByCurrentPage(currentPage,pageSize);
        //将会员信息写入excel文件中
        File file = ListToExcel(list);
        //下载到客户端电脑
        return MyFileUtils.download(file);
    }

    //将集合数据写到excel文件中
    private File ListToExcel(List<Member> list){
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("会员信息");
        //设置表头
        HSSFRow headRow = sheet.createRow(0);
        //设置单元格
        headRow.createCell(0).setCellValue("用户姓名");
        headRow.createCell(1).setCellValue("性别");
        headRow.createCell(2).setCellValue("出生日期");
        headRow.createCell(3).setCellValue("身份证号");
        headRow.createCell(4).setCellValue("手机号码");
        headRow.createCell(5).setCellValue("注册时间");
        headRow.createCell(6).setCellValue("邮箱地址");
        //遍历集合，将集合数据写入excel文件中
        for (int i=0;i<list.size();i++){
            HSSFRow row = sheet.createRow(i + 1);
            Member member = list.get(i);
            row.createCell(0).setCellValue(member.getName());
            row.createCell(1).setCellValue(member.getSex());
            //localDate转String
            String birthday = member.getBirthday().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            row.createCell(2).setCellValue(birthday);
            row.createCell(3).setCellValue(member.getIdcard());
            row.createCell(4).setCellValue(member.getPhonenumber());
            //localDate转String
            String regTime = member.getRegtime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            row.createCell(5).setCellValue(regTime);
            row.createCell(6).setCellValue(member.getEmail());
        }
        File file = new File(path+"会员信息.xls");
        try {
            workbook.write(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }

    @RequestMapping("/memberVo")
    public Result memberVo(){
        try {
            MemberVo memberVo = memberService.listMemberVo();
            return new Result(true,MessageConstant.QUERY_MEMBER_SUCCESS,memberVo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
}

